package com.qire.manhua.model.enumeration;

import com.qire.common.collect.ListUtil;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.model.entity.HomeModuleEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleItemEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleAdvertEntity;
import com.qire.manhua.model.viewModel.impl.RecommendModuleAdvertViewModel;
import com.qire.manhua.model.viewModel.impl.RecommendModuleRankViewModel;
import com.qire.manhua.model.viewModel.impl.RecommendModuleRecordViewModel;
import com.qire.manhua.model.viewModel.impl.RecommendModuleViewModel;

import java.util.ArrayList;

/**
 * 首页推荐选项卡页中子布局模块类型：
 */
public enum HomeModuleType {

    TYPE_2X3(1) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {

            RecommendModuleViewModel moduleViewModel = new RecommendModuleViewModel(homeModuleEntity);

            final ArrayList<HomeModuleItemEntity> itemEntities = homeModuleEntity.moduleItemList();

            if(ListUtil.notEmpty(itemEntities)) {
                for(int index = 0;index < 6; index++) {
                    if(!ListUtil.range(itemEntities, index)) {
                        break;
                    }
                    moduleViewModel.addModelItem(moduleViewModel.new ModuleThreeColumnItemViewModel(itemEntities.get(index), index%3 == 2));
                }
            }

            return new AutoVariable[]{moduleViewModel, moduleViewModel.buildBottomMenu()};
        }
    },
    TYPE_2X2(2) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {
            RecommendModuleViewModel moduleViewModel = new RecommendModuleViewModel(homeModuleEntity);

            final ArrayList<HomeModuleItemEntity> itemEntities = homeModuleEntity.moduleItemList();

            if(ListUtil.notEmpty(itemEntities)) {
                for(int index = 0;index < 4; index++) {
                    if(!ListUtil.range(itemEntities, index)) {
                        break;
                    }
                    moduleViewModel.addModelItem(moduleViewModel.new ModuleTwoColumnItemViewModel(itemEntities.get(index), index%2 == 1));
                }
            }

            return new AutoVariable[]{moduleViewModel, moduleViewModel.buildBottomMenu()};
        }
    },
    TYPE_1P3(3) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {
            RecommendModuleViewModel moduleViewModel = new RecommendModuleViewModel(homeModuleEntity);

            final ArrayList<HomeModuleItemEntity> itemEntities = homeModuleEntity.moduleItemList();

            if(ListUtil.notEmpty(itemEntities)) {
                moduleViewModel.addModelItem(moduleViewModel.new ModuleOneColumnItemViewModel(itemEntities.get(0)));

                for(int index = 1;index < 4; index++) {
                    if(!ListUtil.range(itemEntities, index)) {
                        break;
                    }
                    moduleViewModel.addModelItem(moduleViewModel.new ModuleThreeColumnItemViewModel(itemEntities.get(index), index%3 == 0));
                }
            }

            return new AutoVariable[]{moduleViewModel, moduleViewModel.buildBottomMenu()};
        }
    },
    TYPE_RECORD(4) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {
            RecommendModuleRecordViewModel moduleViewModel = new RecommendModuleRecordViewModel(homeModuleEntity);
            return new AutoVariable[]{moduleViewModel};
        }
    },
    TYPE_AD(5) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {
            HomeModuleAdvertEntity advertEntity = homeModuleEntity.advertEntity();
            if(advertEntity.isShow) {
                RecommendModuleAdvertViewModel moduleViewModel = new RecommendModuleAdvertViewModel(advertEntity);
                return new AutoVariable[]{moduleViewModel};
            } else {
                return null;
            }
        }
    },
    TYPE_RANK(6) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {
            RecommendModuleRankViewModel moduleViewModel = new RecommendModuleRankViewModel(homeModuleEntity);
            return new AutoVariable[]{moduleViewModel};
        }
    },
    TYPE_GUESS(7) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {
            RecommendModuleViewModel moduleViewModel = new RecommendModuleViewModel(homeModuleEntity);

            final ArrayList<HomeModuleItemEntity> itemEntities = homeModuleEntity.moduleItemList();

            if(ListUtil.notEmpty(itemEntities)) {
                for(int index = 0;index < 6; index++) {
                    if(!ListUtil.range(itemEntities, index)) {
                        break;
                    }
                    moduleViewModel.addModelItem(moduleViewModel.new ModuleThreeColumnItemViewModel(itemEntities.get(index), index%3 == 2));
                }
            }

            return new AutoVariable[]{moduleViewModel};
        }
    },
    TYPE_NONE(0) {
        @Override
        public AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity) {
            return new AutoVariable[]{};
        }
    };

    public final int direction;

    HomeModuleType(int direction) {
        this.direction = direction;
    }

    public abstract AutoVariable[] buildModuleViewModels(HomeModuleEntity homeModuleEntity);

    public static HomeModuleType build(int direction) {
        switch (direction) {
            case 1:
                return TYPE_2X3;
            case 2:
                return TYPE_2X2;
            case 3:
                return TYPE_1P3;
            case 4:
                return TYPE_RECORD;
            case 5:
                return TYPE_AD;
            case 6:
                return TYPE_RANK;
            case 7:
                return TYPE_GUESS;
            default:
                return TYPE_NONE;
        }
    }

}
